<?php

/**
 * @file datefield.php
 * @brief Clase para generar un campo fecha
 * @author DyrTechnologies
 * @version V 1.0
 * @date Wed Oct 03 2007
 *
 *  Part of the CEFIRO dynamic web framework project
 *
 *  Copyright (C) 2007  Dyr technologies http://dyrtech.com.ve
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @class Clase datefield
 */
if (!defined('CEFIRO')) define("CEFIRO",substr(__FILE__,0,strpos(__FILE__,"cefiro"))."cefiro/widgets/cefiro.php");
require_once(CEFIRO);

require_once(CEF_INPUT_OPT);
require_once(CEF_DOMAINS);
require_once(CEF_IMAGE);
/**
 * @brief Define la ruta a la hoja de estilos para datefield
 *
 */
define("DATEFIELD_CSS",FULL_URL."datefield/css/datefield.css");
define("CALENDAR_CSS",FULL_URL."datefield/css/calendar.css");
define("CALENDAR_JS",FULL_URL."datefield/js/calendar.js");
define("CALENDAR_ES_JS",FULL_URL."datefield/js/calendar-es.js");
define("CALENDAR_SETUP_JS",FULL_URL."datefield/js/calendar-setup.js");
define("CALENDAR_IMG",FULL_URL."datefield/img/calendar.png");

function get_calendar_scripts(){
	$CJS = "<script type=\"text/javascript\" src=\"".CALENDAR_JS."\"></script>";
	$CEJS = "<script type=\"text/javascript\" src=\"".CALENDAR_ES_JS."\"></script>";
	$CSJS = "<script type=\"text/javascript\" src=\"".CALENDAR_SETUP_JS."\"></script>";
	return $CJS.$CEJS.$CSJS;
}

function get_calendar_src(){
	return CALENDAR_JS;
}

function get_calendar_es_src(){
	return CALENDAR_ES_JS;
}

function get_calendar_setup_src(){
	return CALENDAR_SETUP_JS;
}

/**
 * @brief Nombre alternativo de la imagen con el calendario
 *
 */
define("CALENDAR_ALT","Calendario");

global $datefield_instance;

$datefield_instance =0;

class datefield extends input_opt {

	private $value;
	private $name;
	private $label;
	private $change;
	private $domain;
	private $with_time;
	private $readonly_cal;

	/**
	 * @brief Constructor por omision y parametrico de la clase aprovechamos el hecho de que cada atributo tiene un valor por omision predefinido para no rehacer el constructor
	 *
	 * @param string $_value
	 * @param string $_label
	 * @param string $_name
	 * @param string $_change
	 * @param input_opt $_opt
	 * @param domain $_dom
	 * @return datefield La instancia de la clase
	 */

	public function datefield($_name='__datefield', $_label='', $_value='', $_change=true, $_with_time=false, $_opt = '', $_dom = 'A1', $_readonly_cal = false){

		/**
		 * @brief Se asignan los valores a los atributos de la clase datefield segun vengan dados al llamar al constructor de la misma, notar que existen valores por omision para cada atributo
		 */
		$this->value=$_value;
		$this->label=$_label;
		$this->change=$_change;
		$this->with_time=$_with_time;
		if($_readonly_cal)
			$this->readonly_cal = "true";
		else $this->readonly_cal = "false";

		/**
		 * @brief $datefield_instance es una variable para incrementar el numero de campos fechas existentes
		 */
		global $datefield_instance;
		$datefield_instance++;

		/**
		 * @brief El nombre del widget es el tipo con el numero de la instancia que representa o el nombre pasado por omision en caso contrario
		 */

		if($_name!="__datefield")
		$this->name=$_name;
		else
		$this->name="datefield".$datefield_instance;

		/**
		 * @brief Si las opciones del campo no estan ajustadas lo hacemos explicitamente una instancia de input_opt
		 */
		if($_opt=='')
		$_opt = new input_opt();
		$this->equalize($_opt);

		$this->domain = new domain();

		if($_dom!='')
		$this->set_domain($_dom);
	}


	/**
	 * @brief Las funciones get_VAR devuelven las variables que tenga ajustadas la instancia: valor (value), Nombre (name), Etiqueta (label), Change, opciones de entrada (opt)
	 *
	 * @return string Valor de VAR
	 */

	public function get_name(){
		return $this->name;
	}

	public function get_value(){
		return $this->value;
	}

	public function get_label(){
		return $this->label;
	}

	public function get_change(){
		return $this->change;
	}

	public function get_opt(){
		return $this->get_input_opt();
	}

	public function get_time(){
		return $this->with_time;
	}

	public function get_readonly_cal(){
		return $this->readonly_cal;
	}


	/**
	 * @brief Las funciones set_VAR ajustan las variables de la instancia: valor (value), Nombre (name), Etiqueta (label), Change, opciones de entrada (opt)
	 */

	public function set_name($_name){
		$this->name=$_name;
	}

	public function set_value($_value){
		$this->value=$_value;
	}

	public function set_label($_label){
		$this->label=$_label;
	}

	public function set_change($_change){
		$this->change=$_change;
	}

	public function set_opt($_opt=''){
		if($_opt=='')
		$_opt = new input_opt();
		$this->equalize($_opt);
	}

	public function set_readonly_cal($_readonly = false){
		if($_readonly)
			$this->readonly_cal = "true";
		else
			$this->readonly_cal = "false";
	}



	/**
	 * @brief Reeimplementamos las funciones del dominios... para facilidad al codificar en el nivel de interfaz
	 */

	public function set_domain($nu_domain){
		$this->domain->set_domain($nu_domain);
	}

	public function reset_domain(){
		$this->domain->reset();
	}

	public function add_domain($nu_domain){
		$this->domain->add_domain($nu_domain);
	}

	public function add_domains($nu_domains){
		$this->domain->add_domains($nu_domains);
	}

	public function remove_domain($nu_domain){
		$this->domain->remove_domain($nu_domain);
	}

	public function remove_domains($nu_domains){
		$this->domain->remove_domains($nu_domains);
	}

	public function get_domains(){
		return $this->domain->get_domains();
	}


	/**
 	* @brief Extiende el calendario con fecha y hora
 	*/

	public function set_time($_time=''){
		if($_time)
		{
				$this->with_time = "true";
		}
	}




	/**
	 * @brief Ajusta los valores de smarty de acuerdo a lo especificado por los atributos de la instancia datefield
	 */
	private function smarty_datefield(){

		global $smarty;

		/**
		 * @brief Asignacion atributo por atributo a lo deseado en las variables de smarty
		 */
		$smarty->assign("name",$this->get_name());
		$smarty->assign("value",$this->get_value());
		$smarty->assign("label",$this->get_label());
		$smarty->assign("cad_opt",$this->get_input_opt());
		$smarty->assign("cad_domain",$this->get_domains());
		$smarty->assign("change",$this->get_change());
 		$smarty->assign("time",$this->with_time);
 		$smarty->assign("readonly_cal",$this->get_readonly_cal());

		global $datefield_instance;
		$smarty->assign("numinstance",$datefield_instance);

		if($this->change){

			$cal_opt = new input_opt();
			$cal_opt->set_extra("id=\"cal_{$this->name}\" align=\"top\"");

			$calendar = new imagece($this->get_name(),CALENDAR_IMG,CALENDAR_ALT,'');
			$calendar->set_opt($cal_opt);
			$calendar=$calendar->display_image(true);
			$smarty->assign("calendar_img",$calendar);

		}

		/**
		 * @brief Asigna la hoja de estilo que contiene los rasgos del campo fecha a una variable
		 */
		$smarty->assign("datefieldcss",DATEFIELD_CSS);
		$smarty->assign("calendarcss",CALENDAR_CSS);

		/**
		 * @brief Asigna los javascripts del calendario
		 */
		$smarty->assign("calendarjs",CALENDAR_JS);
		$smarty->assign("calendar_esjs",CALENDAR_ES_JS);
		$smarty->assign("calendar_setupjs",CALENDAR_SETUP_JS);
	}


	/**
	 * @brief Asigna el datefield a la variable $smarty para su renderizacion
	 *
	 */
	public function display_datefield($print_scripts = true, $fetch = false){

		global $smarty;
		//$this->set_onChange("zajax.validatom(this)");
		$this->smarty_datefield();
		if($print_scripts)
		  $smarty->assign("scripts","true");
		else
		  $smarty->assign("scripts","false");
		$smarty->template_dir = dirname(__FILE__).'/';
		if ($fetch)
		  return $smarty->fetch('datefield.tpl');
		else{
		  $smarty->assign( $this->name , $smarty->fetch('datefield.tpl') );
      return null;
		}
	}

	public function shortScripts(){
	  $na = $this->get_name();
		return "Calendar.showsTime=true; Calendar.setup({inputField:\"$na\",button:\"cal_$na\",ifFormat:\"%d/%m/%Y\",showsTime:false}); Calendar.setup({inputField:\"$na\",button:\"$na\",ifFormat:\"%d/%m/%Y\",showsTime:false,eventName:\"focus\"});";
	}

	/**
	 * @brief Devuelve el setup del calendario
	 *
	 */
	public function get_scripts_setups($cal_instance = null){

	  if (isset($cal_instance))
	  	$cal_instance .= '.Calendar';
	  else
	    $cal_instance = 'Calendar';

		$cal_setup_script = "
			$cal_instance.showsTime = true;

			$cal_instance.setup	(
								{
									inputField:\"".$this->get_name()."\",
									button:\"cal_".$this->get_name()."\",";
		if($this->get_time()){
		$cal_setup_script.="
									ifFormat:\"%m/%d/%Y %I:%M %p\",
									showsTime:true,
									timeFormat:\"12\"";
		} else{
		$cal_setup_script.="
									ifFormat:\"%d/%m/%Y\",
									showsTime:false";
		}

		$cal_setup_script.="});

			$cal_instance.setup	(
								{
									inputField: \"".$this->get_name()."\",
									eventName: \"focus\",
									button: \"".$this->get_name()."\",";

		if($this->get_time()){
		$cal_setup_script.="
									ifFormat:\"%m/%d/%Y %I:%M %p\",
									showsTime:true,
									timeFormat:\"12\"";
		} else{
		$cal_setup_script.="
									ifFormat:\"%d/%m/%Y\",
									showsTime:false";
		}
		$cal_setup_script.="});";

		return $cal_setup_script;
	}
}

 ?>